package com.john.myokhttpimagerecycledemo;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.squareup.picasso.MemoryPolicy;
import com.squareup.picasso.Picasso;
import com.squareup.picasso.Transformation;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * author: John
 * time: 2016/7/15
 * overview:
 * usage:
 */
public class PicassoActivity extends Activity {
    @Bind(R.id.iv_picasso)
    ImageView ivPicasso;
    @Bind(R.id.btn_result)
    Button btnResult;
    @Bind(R.id.btn_assets)
    Button btnAssets;


    private String url="http://img1.imgtn.bdimg.com/it/u=2115941808,2049013393&fm=206&gp=0.jpg";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_picasso);
        ButterKnife.bind(this);
        Picasso.with(this).load("http://att.bbs.duowan.com/forum/201512/02/214048jo9tua9q197p9tjf.jpg").into(ivPicasso);
    }


    @OnClick({R.id.btn_result, R.id.btn_assets})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_result:
                Picasso.with(this)
                        .load(url)
                        .placeholder(R.mipmap.timg)
                        .error(R.mipmap.ic_launcher)
                        .noFade()
                        .transform(new Transformation() {
                            @Override
                            public Bitmap transform(Bitmap source) {
                                int size=Math.min(source.getWidth(),source.getHeight());
                                Bitmap result=Bitmap.createBitmap(source,0,0,size,size);
                                if (result!=source){
                                    source.recycle();
                                }
                                return result;
                            }

                            @Override
                            public String key() {
                                return "square";
                            }
                        })
                        .rotate(90)
                        .config(Bitmap.Config.RGB_565)
                        .memoryPolicy(MemoryPolicy.NO_STORE)
                        .into(ivPicasso);
                break;
            case R.id.btn_assets:
                Picasso.with(this).load("file:///android_asset/sceney.jpg")
                        .config(Bitmap.Config.RGB_565)
                        .into(ivPicasso);
                break;
        }
    }
}
